package com.snakerflow.framework.security.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.snakerflow.framework.orm.Page;
import com.snakerflow.framework.orm.PropertyFilter;
import com.snakerflow.framework.security.dao.PositionDao;
import com.snakerflow.framework.security.entity.Position;

/**
 * 岗位管理类
 * @author yuqs
 * @since 0.1
 */
@Component
public class PositionManager {
	//注入岗位持久化对象
	@Autowired
	private PositionDao positionDao;
	
	/**
	 * 保存岗位实体
	 * @param entity
	 */
	public void save(Position entity) {
		positionDao.save(entity);
	}
	
	/**
	 * 根据主键ID删除对应的岗位
	 * @param id
	 */
	public void delete(Long id) {
		positionDao.delete(id);
	}
	
	/**
	 * 根据主键ID获取岗位实体
	 * @param id
	 * @return
	 */
	public Position get(Long id) {
		return positionDao.get(id);
	}
	
	/**
	 * 根据分页对象、过滤集合参数，分页查询岗位列表
	 * @param page
	 * @param filters
	 * @return
	 */
	public Page<Position> findPage(final Page<Position> page, final List<PropertyFilter> filters) {
		return positionDao.findPage(page, filters);
	}
	
	/**
	 * 获取所有岗位记录
	 * @return
	 */
	public List<Position> getAll() {
		return positionDao.getAll();
	}
	
	/**
	 * 根据parentId获取所有下级岗位
	 * @param parentId
	 * @return
	 */
	public List<Position> getByParent(Long parentId) {
		if(parentId == null || parentId == Position.ROOT_POS_ID) {
			return getAll();
		}
		return positionDao.find("from Position pos where pos.parentPos=?", new Position(parentId));
	}
	
	public String getMaxCode(){
		List list = positionDao.find("SELECT max(code) FROM Position", new Object[]{});
		if(list != null && !list.isEmpty())
			return ((Position) list.get(0)).getCode();
		return "A";
	}
}
